package com.zeei.ems.base.service.impl;

import com.zeei.ems.base.dao.DeptInfoDao;
import com.zeei.ems.base.service.DeptInfoService;
import com.zeei.ems.base.tree.TreeNodeVO;
import com.zeei.ems.base.util.TreeUtil;
import com.zeei.ems.base.vo.DeptInfoVO;
import com.zeei.ems.base.vo.QueryDeptParamVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DeptInfoServiceImpl implements DeptInfoService {

	@Autowired
	DeptInfoDao deptInfoDao;

	@Override
	public List<TreeNodeVO<DeptInfoVO>> queryDeptInfo(QueryDeptParamVO queryDeptParam) {
		Map<String, Object> condition = queryDeptParam.convertConditionMap();
		List<DeptInfoVO> list = deptInfoDao.queryDeptInfoByCondition(condition);
		String deptId = "";
		List<TreeNodeVO<DeptInfoVO>> resultList;
		if (StringUtils.isNotEmpty(queryDeptParam.getSearchDeptName())) {
			List<DeptInfoVO> allList = deptInfoDao.queryDeptInfoByCondition(new HashMap<String, Object>());
			List<TreeNodeVO<DeptInfoVO>> treeList = TreeUtil.buildSearchNodeTree(allList, list);
			resultList = treeList;
		} else {
			resultList = TreeUtil.buildTreeNodeTree(list, deptId);
		}

		return resultList;
	}
}
